package com.groupscore.score.service;


import com.groupscore.score.dao.UserExceptionDao;
import com.groupscore.score.pojo.UserException;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class UserExcepitionService {

    @Autowired
    private UserExceptionDao userExceptionDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FeignUserService feignUserService;

    public void save(UserException userException){

        Result result = feignUserService.findNameById( userException.getUserId() );
        String userName = (String) result.getData();

        userException.setUserName( userName );

        userException.setId( idWorker.nextId()+"" );
        userExceptionDao.save( userException );
    }

    public void deleteById(Integer id){
        userExceptionDao.deleteById( id );
    }

    /**
     * 确定后修改状态
     */
    public void update(UserException userException){
        userExceptionDao.save( userException );
    }

    public List<UserException> findAll(){
        return userExceptionDao.findAllOrOrderByCreateTime();
    }

    public UserException findById(String id){
        return userExceptionDao.findById( id );
    }

    public List<UserException> findByUserId(String userId){
        return userExceptionDao.findAllByUserId(userId);
    }

    /**
     * 条件查询+分页
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<UserException> findSearch(Map whereMap, int page, int size) {
        Specification<UserException> specification = createSpecification(whereMap);
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        return userExceptionDao.findAll(specification, pageRequest);
    }

    /**
     * 动态条件构建
     * @param searchMap
     * @return
     */
    private Specification<UserException> createSpecification(Map searchMap) {

        return new Specification<UserException>() {

            @Override
            public Predicate toPredicate(Root<UserException> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 标题
                if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
                    predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
                }
                //章节id
                if (searchMap.get("chapterId")!=null && !"".equals(searchMap.get("chapterId"))) {
                    predicateList.add(cb.equal(root.get("chapterId").as(Integer.class), (Integer)searchMap.get("chapterId")));
                }
                // 章节名称
                if (searchMap.get("chapterName")!=null && !"".equals(searchMap.get("chapterName"))) {
                    predicateList.add(cb.like(root.get("chapterName").as(String.class), "%"+(String)searchMap.get("chapterName")+"%"));
                }

                //分类id
                if (searchMap.get("catId")!=null && !"".equals(searchMap.get("catId"))) {
                    predicateList.add(cb.equal(root.get("catId").as(Integer.class), (Integer)searchMap.get("catId")));
                }
                // 分类名称
                if (searchMap.get("catName")!=null && !"".equals(searchMap.get("catName"))) {
                    predicateList.add(cb.like(root.get("catName").as(String.class), "%"+(String)searchMap.get("catName")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }
}
